import {ref} from 'vue'

import { postax, getax } from "../common/request/index"


/**
 * 获取Home混合信息(轮播图，每日更新，完结日漫，热门国漫)
 * @returns 所有Home页面data
 */
export async function getHomeMixData() {
    try {
        // 新番上映月份
        // 0-1 3-4 6-7 9-10
        const newAnimeTable = [0,3,6,9]
        // 当前年份
        const year = new Date().getFullYear()
        // 当前番剧月份
        const mouth = newAnimeTable.filter((item)=> 0 <= new Date().getMonth() - item  && new Date().getMonth() - item < 3)
        const newAnime = year + '-' + mouth + '-' + 28
        // 每周动漫表
        const perweek = {
            '0': [],
            '1': [],
            '2': [],
            '3': [],
            '4': [],
            '5': [],
            '6':[]
        }
        const {data:carousel} = await getax('api/carousel/slideshowQuery')
        const {data:theatreAnime} = await postax('api/anime/MultiConditionQuery', {
            animeType:3,
            mouthHot: 1,
            pageSize: 5,
            r18:0,
        })
        const {data:japanAnime} = await postax('api/anime/MultiConditionQuery', {
            isAnimeOrCartoon: 0,
            isOver: 1,
            r18:0,
            mouthHot: 1,
            pageSize: 4
        })
        const {data:chineseCartoon} = await postax('api/anime/MultiConditionQuery', {
            isAnimeOrCartoon: 1,
            isOver: 1,
            r18:0,
            mouthHot: 0,
            pageSize: 4
        })
        
        const { data: res } = await postax('api/anime/MultiConditionQuery', {
            uploadYear: newAnime,
            r18:0,
            pageSize:9999
        })
        
        console.log(newAnime)
        res.data.list.forEach(item => {
            const day = new Date(item.publishYear).getDay()
            perweek[`${day}`].push(item)
        })
        

        return {
            carousel,
            perweek,
            japanAnime,
            chineseCartoon,
            theatreAnime
        } 
    }
    catch {
        console.log('error')
    }
}

/**
 * 获取视频播放页面混合新
 * @param {*动漫信息} AnimeMsg 
 * @param {*视频} VideoSrc 
 * @param {*动漫id} id 
 */
export function getVideoMixData(id) {
    const AnimeMsg = ref([])
    const VideoSrc = ref([])
    // const AnimeTag = ref([])
        
    const anime = async () => {
        const [urls, msg] = await Promise.all([
            getax(`api/video/getByAnimeId/${id}`),
            getax(`api/anime/getById/${id}`)

        ])
        AnimeMsg.value = msg.data.data
        VideoSrc.value = urls.data.data
    }
    anime()
    
    return {
        AnimeMsg,
        VideoSrc,
    }
}

/**
 * 获取动漫标签
 * @param {*动漫id} id 
 * @param {*回调} callback 
 */
export async function getAnimeTag(id,callback) {
    const {data:res} = await getax(`api/tag/getAnimeTagName/${id}`)
    callback(res)
}
